package gitee.derumin.really_forge.mixin;

import gitee.derumin.really_forge.items.HammerItem;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(PlayerEntity.class)
public abstract class PlayerEntityMixin extends LivingEntity {

    protected PlayerEntityMixin(EntityType<? extends LivingEntity> entityType, World world) {
        super(entityType, world);
    }

    @Inject(method = "attack", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;onAttacking(Lnet/minecraft/entity/Entity;)V"), locals = LocalCapture.CAPTURE_FAILHARD)
    private void process(Entity target, CallbackInfo ci, float f, float g, float h){
        ItemStack stack = this.getMainHandStack();
        if (stack.getItem() instanceof HammerItem hammer){
            double power = h * hammer.getKnockBackPower(stack);
            if (target instanceof LivingEntity livingEntity){
                livingEntity.takeKnockback(power*0.5f, MathHelper.sin(this.getYaw() * 0.017453292f), -MathHelper.cos(this.getYaw() * 0.017453292f));
            }else {
                target.addVelocity(-MathHelper.sin(this.getYaw() * 0.017453292f) * power * 0.5f,0.1f, MathHelper.cos(this.getYaw() * 0.017453292f) * power * 0.5f);
            }
        }
    }
}
